package com.example.taobaounion.ui.adapter

import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.example.taobaounion.model.bean.Data


/**
 *@author : Administrator
 *@descreption : HomeFragment的适配器
 */
class HomeViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
    fun setData(list: List<Data>){
        this.categoryList = list
        notifyDataSetChanged()
    }
    private var categoryList: List<Data> =  ArrayList()
    override fun getPageTitle(position: Int): CharSequence? {
        return categoryList[position].title
    }

    override fun getItem(position: Int): Fragment {
       // Log.i("TAG", "类目是:=>>>>>>${categoryList[position].title} ");
        return CategoryFragment.newInstance(categoryList[position].id,categoryList[position].title)

    }

    override fun getCount(): Int {
        return categoryList.size
    }



    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        //注释掉父类的destroyItem可以防止viewpager在滑动的时候fragment被销毁然后重新加载
        //注释掉后就不会重新加载fragment
       // super.destroyItem(container, position, `object`)
    }
}